package dao

import (
	"fmt"
	"time"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/kaproject"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// -------------------- dao层 - 网点相关操作 -----------------------//

type KaProjectDao struct {
}

func NewKaProjectDao() *KaProjectDao {
	return &KaProjectDao{}
}

//  获取项目列表API
func (d *KaProjectDao) GetKaProjectList(param *request.KaProjectListReq) *dingo.ErrorWrap {

	var (
		lst    []*kaproject.KaProjectModel
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	db := global.Orm.Debug().Table("ka_project")

	if param.KaLabel != "" { // 查询条件：唯一标识
		db.Where("ka_label = ?", param.KaLabel)
	}

	if param.KaName != "" { //项目名称
		db.Where("ka_name like ?", fmt.Sprint("%", param.KaName, "%"))
	}
	if param.Status > 0 { //状态
		db.Where("status = ?", param.Status)
	}

	//db.Order("create_at desc")

	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)

	return dingo.ErrorIf(db.Error, lst, count)
}

//  获取项目列表API
func (d *KaProjectDao) GetAllKaProjectList() *dingo.ErrorWrap {

	var lst []*kaproject.KaProjectModel
	db := global.Orm.Debug().Table("ka_project").Find(&lst)

	return dingo.ErrorIf(db.Error, lst)
}

//  获取项目列表API
func (d *KaProjectDao) InsertKaProjectInfo(param *request.KaProjectInsertReq) *dingo.ErrorWrap {
	db := global.Orm
	k := kaproject.New().Mutate(
		kaproject.WithCreateAt(dingo.Time(time.Now().Unix())),
		kaproject.WithKaLabel(param.KaLabel),
		kaproject.WithKaName(param.KaName),
		kaproject.WithStatusc(param.Status),
		kaproject.WithKaDesc(param.KaDesc),
		kaproject.WithCouponId(param.CouponId),
		kaproject.WithMembersUrl(param.MembersUrl),
		kaproject.WithMembersTestUrl(param.MembersTestUrl),
		kaproject.WithAppid(param.Appid),
		kaproject.WithAppSecret(param.AppSecret),
		kaproject.WithIsToken(param.IsToken),
		kaproject.WithCode(param.Code),
	)
	db.Debug().Table("ka_project").Create(&k)
	return dingo.ErrorIf(db.Error, k)
}

//  获取项目列表API
func (d *KaProjectDao) UpdateKaProjectInfo(param *request.KaProjectInsertReq) *dingo.ErrorWrap {
	update := map[string]interface{}{
		"update_at":        dingo.Time(time.Now().Unix()),
		"ka_label":         param.KaLabel,
		"ka_name":          param.KaName,
		"status":           param.Status,
		"ka_desc":          param.KaDesc,
		"coupon_id":        param.CouponId,
		"members_url":      param.MembersUrl,
		"members_test_url": param.MembersTestUrl,
		"appid":            param.Appid,
		"appsecret":        param.AppSecret,
		"is_token":         param.IsToken,
		"code":             param.Code,
	}
	db := global.Orm.Debug().Table("ka_project").Where("id=?", param.Id).Updates(update)
	return dingo.ErrorIf(db.Error, true)
}

//  根据ID删除信息
func (d *KaProjectDao) DeleteKaProjectById(id int) *dingo.ErrorWrap {
	db := global.Orm.Exec("delete from ka_project where id = ?", id)
	return dingo.ErrorIf(db.Error, true)
}

func (d *KaProjectDao) GetKaProjectInfoByKaLabel(kaLabel string) *dingo.ErrorWrap {
	m := kaproject.KaProjectModel{}
	db := global.Orm.Debug().Table("ka_project").Where("ka_label = ?", kaLabel).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}
